package com.zsk.shop.service.yulink;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsk.shop.common.utils.DateUtils;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.ShopOrderPoints;
import com.zsk.shop.entity.ShopOrderPointsDetail;
import com.zsk.shop.entity.ShopOrderPointsToAccount;
import com.zsk.shop.entity.ShopUser;
import com.zsk.shop.entity.yulink.LinkShopLogInfo;
import com.zsk.shop.enums.EnumOrderPointsStatus;
import com.zsk.shop.enums.EnumRatioWithLv;
import com.zsk.shop.service.ShopOrderPointsDetailService;
import com.zsk.shop.service.ShopOrderPointsService;
import com.zsk.shop.service.ShopOrderPointsToAccountService;
import com.zsk.shop.service.ShopUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ljh
 * @create 2022/2/22 11:29
 */
@Service
public class YuLinkService {

    private static final Logger logger = LoggerFactory.getLogger(YuLinkService.class);

    @Resource
    private YlUserService ylUserService;
    @Resource
    private LinkShopLogInfoService linkShopLogInfoService;
    @Resource
    private ShopUserService shopUserService;
    @Resource
    private ShopOrderPointsService shopOrderPointsService;
    @Resource
    private ShopOrderPointsDetailService shopOrderPointsDetailService;
    @Resource
    private ShopOrderPointsToAccountService shopOrderPointsToAccountService;

    /**
     * 统计昨天的登录数据至订单解冻积分列表
     */
    public void handleLoginLinkPointDetail(Date day) {
        LambdaQueryWrapper<LinkShopLogInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LinkShopLogInfo::getIsHandle, false);
        queryWrapper.le(LinkShopLogInfo::getCreateTime, DateUtils.getDateEnd(day));
        queryWrapper.ge(LinkShopLogInfo::getCreateTime, DateUtils.getDateStart(day));
        List<LinkShopLogInfo> listAll = linkShopLogInfoService.list(queryWrapper);
        List<String> userPhoneList = listAll.stream().map(LinkShopLogInfo::getPhone).collect(Collectors.toList());
        List<ShopUser> shopUserList = shopUserService.listByPhones(userPhoneList);
        List<String> userNoList = shopUserList.stream().map(ShopUser::getUserNo).collect(Collectors.toList());
        List<ShopOrderPoints> shopOrderPointsListAll = shopOrderPointsService.listByUserNosWithNoThaw(userNoList);
        for (LinkShopLogInfo linkShopLogInfo : listAll) {
            BigDecimal loginRatio = linkShopLogInfo.getHaveTenOrSubUser().equals(2) ? new BigDecimal("0.01") :
                    (linkShopLogInfo.getHaveTenOrSubUser().equals(1) ? new BigDecimal("0.005") : BigDecimal.ZERO);
            if (loginRatio.equals(BigDecimal.ZERO)) {
                continue;
            }
            ShopUser shopUser = shopUserList.stream().filter(x -> x.getPhone().equals(linkShopLogInfo.getPhone())).findFirst().orElse(new ShopUser());
            if (ToolUtil.isEmpty(shopUser)) {
                logger.info("当前数据未匹配解冻数据" + JSONObject.toJSONString(linkShopLogInfo));
                continue;
            } else {
                BigDecimal pointRatio = EnumRatioWithLv.getThawPointRatioByLv(shopUser.getLv());
                BigDecimal moneyRatio = EnumRatioWithLv.getThawAccountRatioByLv(shopUser.getLv());
                List<ShopOrderPoints> shopOrderPointsList = shopOrderPointsListAll.stream()
                        .filter(x -> x.getUserNo().equals(shopUser.getUserNo())).collect(Collectors.toList());
                for (ShopOrderPoints shopOrderPoints : shopOrderPointsList) {
                    //【本身加上解冻数据】
                    BigDecimal point = shopOrderPoints.getPoints();
                    if (shopOrderPoints.getRatio().add(loginRatio).equals(new BigDecimal(1))) {
                        shopOrderPoints.setStatus(EnumOrderPointsStatus.SUCCESS.getCode());
                        shopOrderPoints.setThawTime(new Date());
                    } else if (shopOrderPoints.getRatio().add(loginRatio).compareTo(new BigDecimal(1)) >= 1) {
                        loginRatio = shopOrderPoints.getRatio().add(loginRatio).subtract(new BigDecimal(1)).setScale(3, BigDecimal.ROUND_HALF_UP);
                        shopOrderPoints.setStatus(EnumOrderPointsStatus.SUCCESS.getCode());
                        shopOrderPoints.setThawTime(new Date());
                    }
                    shopOrderPoints.setRatio(shopOrderPoints.getRatio().add(loginRatio));
                    BigDecimal thawTodayPoint = point.multiply(loginRatio);
                    //已转换总积分
                    shopOrderPoints.setThawTotalPoint(shopOrderPoints.getThawTotalPoint().add(thawTodayPoint));
                    BigDecimal thawPoint = thawTodayPoint.multiply(pointRatio);
                    //已转换积分到账的积分
                    shopOrderPoints.setThawPoint(shopOrderPoints.getThawPoint().add(thawPoint));
                    BigDecimal thawMoney = thawTodayPoint.multiply(moneyRatio).setScale(2, BigDecimal.ROUND_DOWN);
                    //已转换积分到账的钱
                    shopOrderPoints.setThawMoney(shopOrderPoints.getThawMoney().add(thawMoney));
                    this.shopOrderPointsService.saveOrUpdate(shopOrderPoints);
                    //【详情加上解冻积分】
                    String orderPointsNo = shopOrderPoints.getOrderPointsNo();
                    ShopOrderPointsDetail shopOrderPointsDetail = ShopOrderPointsDetail.builder()
                            .orderPointsNo(orderPointsNo).ratio(loginRatio).pointRatio(pointRatio)
                            .moneyRatio(moneyRatio).thawPoint(thawPoint)
                            .thawMoney(thawMoney).thawTime(linkShopLogInfo.getCreateTime()).build();
                    this.shopOrderPointsDetailService.save(shopOrderPointsDetail);
                    //【加上解冻金额】
                    ShopOrderPointsToAccount shopOrderPointsToAccount = ShopOrderPointsToAccount.builder()
                            .userNo(shopUser.getUserNo()).sourceBizNo(shopOrderPoints.getSourceOrderNo())
                            .changeMoney(thawMoney).type(1).createTime(linkShopLogInfo.getCreateTime())
                            .isResult(false).build();
                    this.shopOrderPointsToAccountService.save(shopOrderPointsToAccount);
                }
            }
            linkShopLogInfo.setIsHandle(true);
            this.linkShopLogInfoService.saveOrUpdate(linkShopLogInfo);
        }
    }

}
